package com.view
{
	import flash.events.Event;
	
	import fr.seraf.wow.core.WOWEngine;
	import fr.seraf.wow.core.data.WPlane;
	import fr.seraf.wow.core.data.WVector;
	import fr.seraf.wow.primitive.WOWPlane;
	import fr.seraf.wow.primitive.WSphere;
	
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	
	public class SceneSphere extends BasicView
	{
		//Variables
		private var _matt:Plane;
		private var _sphere:Sphere;
		private var _wowSphere:WSphere;
		private var _wowPlane:WOWPlane;
		private var _wow:WOWEngine;
		
		//Constructor
		public function SceneSphere()
		{
			super( 1024, 800 );
			this.initPhysics();
			this.init3D();
		}
		
		//Public methods
		
		//Private methods
		private function initPhysics():void
		{
			this._wow = new WOWEngine();
			this._wow.collisionResponseMode = this._wow.SELECTIVE;
			
			this._wow.addMasslessForce( new WVector( 0, 10, 0 ) );
		}
		
		private function init3D():void
		{
			//3D
			var mWire:WireframeMaterial = new WireframeMaterial( 0x00FF00, 1 );
			this._matt = new Plane( mWire, 800, 800, 5, 5 );
			this._matt.material.doubleSided = true;
			this._matt.rotationX = 90;
			
			this._sphere = new Sphere( mWire, 100, 10, 10 );
			this._sphere.material.doubleSided = true;
			
			this._sphere.y = 400;
			
			scene.addChild( this._sphere );
			scene.addChild( this._matt );
			
			//Physics
			this._wowPlane = new WOWPlane();
		     //this._wowPlane.elasticity = .6;
			//this._wowPlane.friction = .3;
			this._wow.addParticle( this._wowPlane );
			
			this._wowSphere = new WSphere( this._sphere.x, -this._sphere.y, this._sphere.z, 100, false , .1, .5, .02 );
			this._wow.addParticle( this._wowSphere );
			
			this.addEventListener( Event.ENTER_FRAME, updateScene );
		}
		
		//Event handlres
		private function updateScene( $event:Event ):void
		{
			this._wow.step();
			this.singleRender();
			
			this._sphere.x = this._wowSphere.px;
			this._sphere.y = -this._wowSphere.py;
			this._sphere.z = this._wowSphere.pz;
			
			//this._matt.x = this._wowPlane.px;
			//this._matt.y = -this._wowPlane.py;
		   // this._matt.z = this._wowPlane.pz;
			this._wowPlane.setPosition( this._matt.x, -this._matt.y , this._matt.z );
		//	this._wowPlane.setRotation( this._matt.rotationX, this._matt.rotationY, 0);
			
			//this._sphere.yaw(viewport.containerSprite.mouseX * .01 );
			this._matt.y = (viewport.containerSprite.mouseX * .1 );
		}
	}
}